package servlet;

import pojo.Book;
import pojo.Category;
import pojo.Pager;
import dao.BookSql;
import dao.CategoryDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/BookServlet")
@MultipartConfig
public class BookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m=request.getParameter("m");
        if(m.equals("queryBook")){
            try {
                queryBook(request,response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //删除书籍信息
        else if(m.equals("deleteBook")){
            deleteBook(request,response);
        }
        //修改书籍信息
        else if(m.equals("updateBook")){
            updateBook(request,response);
        }
        //跳转到修改书籍页面并显示分类列表
        else if(m.equals("toAddBook")){
            try {
                toAddBook(request,response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //添加书籍信息
        else if(m.equals("addBook")){
            addBook(request,response);
        }
    }

    protected void toAddBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<Category> categoryMsg = CategoryDao.getCategoryMsg();
        request.setAttribute("categoryMsg",categoryMsg);
        System.out.println(categoryMsg);
        request.getRequestDispatcher("addBook.jsp").forward(request,response);
    }

    protected void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String book_name=request.getParameter("book_name");
        String book_author=request.getParameter("book_author");
        String book_category=request.getParameter("book_category");
        String book_price=request.getParameter("book_price");
        String book_datetime=request.getParameter("book_datetime");
        String book_special=request.getParameter("book_spec");
        String book_hot=request.getParameter("book_hot");
        String book_descript=request.getParameter("book_descript");
        Part part=request.getPart("book_photo");
        String filename=part.getSubmittedFileName();
        if(filename.equals("")){
            filename="images/default.jpg";
        }else {
            filename="images/"+System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            part.write(this.getServletContext().getRealPath("/")+filename);
        }
        Book book=new Book();
        book.setSpecial(Integer.parseInt(book_special));
        book.setHot(Integer.parseInt(book_hot));
        book.setDatetime(book_datetime);
        book.setPrice(Double.parseDouble(book_price));
        book.setCategory(book_category);
        book.setAuthor(book_author);
        book.setDescript(book_descript);
        System.out.println(book_descript);
        book.setName(book_name);
        book.setImg(filename);
        Boolean aBoolean = BookSql.addBook(book);
        if(aBoolean==true){
            request.setAttribute("msg","添加书籍信息成功");
            request.getRequestDispatcher("BookServlet?m=queryBook").forward(request,response);
        }else {
            request.setAttribute("msg","填写信息有误");
            request.getRequestDispatcher("BookServlet?m=queryBook").forward(request,response);
        }
    }

    protected void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String book_id=request.getParameter("book_id");
        String book_name=request.getParameter("book_name");
        String book_author=request.getParameter("book_author");
        String book_category=request.getParameter("book_category");
        String book_price=request.getParameter("book_price");
        String book_datetime=request.getParameter("book_datetime");
        String book_special=request.getParameter("book_spec");
        String book_descript=request.getParameter("book_descript");
        String book_hot=request.getParameter("book_hot");
        Part part=request.getPart("book_photo");
        String filename=part.getSubmittedFileName();
        if(filename.equals("")){
            filename="images/default.jpg";
        }else {
            filename="images/"+System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            part.write(this.getServletContext().getRealPath("/")+filename);
        }
        System.out.println(filename);

        Book book=new Book();
        book.setId(Integer.parseInt(book_id));
        book.setSpecial(Integer.parseInt(book_special));
        book.setHot(Integer.parseInt(book_hot));
        book.setDatetime(book_datetime);
        book.setPrice(Double.parseDouble(book_price));
        book.setCategory(book_category);
        book.setAuthor(book_author);
        book.setName(book_name);
        book.setImg(filename);
        book.setDescript(book_descript);
        System.out.println(book);
        if(BookSql.updateBook(book)==true){
            response.sendRedirect("BookServlet?m=queryBook");
        }else {
            request.setAttribute("msg","修改图书信息错误");
            request.getRequestDispatcher("BookServlet?m=queryBook").forward(request,response);
        }


    }

    protected void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        BookSql.deleteBook(id);
        response.sendRedirect("BookServlet?m=queryBook");
    }

    protected void queryBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //获取当前页
        String cp=request.getParameter("currentPage");
        Pager pager=new Pager(cp);
        //获取查询名字
        String bname = request.getParameter("b_name");
        //获取分类
        String bcate=request.getParameter("b_cate");
        String flag="";
        if(bname==null&&bcate!=null){
            BookSql.pageQuery(pager,bcate,flag);
        }
        else if (bcate==null&&bname!=null){
            BookSql.pageQuery(pager,flag,bname);
        }
        else if(bcate==null&&bname==null){
            BookSql.pageQuery(pager,flag,flag);
        }
        else {
            request.setAttribute("bcate",bcate);
            request.setAttribute("bname",bname);
            BookSql.pageQuery(pager,bcate,bname);
        }
        System.out.println(pager.getRs());
        request.setAttribute("pager",pager);
        List<Category> categoryMsg = CategoryDao.getCategoryMsg();
        request.setAttribute("categoryMsg",categoryMsg);
        request.getRequestDispatcher("queryBook.jsp").forward(request,response);
    }
}
